import 'package:flutter/material.dart';
import 'package:wk_weather/myConfig/dimen_constant.dart';
import 'package:wk_weather/myConfig/weather_type.dart';
import 'package:wk_weather/utils/TimeUtil.dart';
import 'package:wk_weather/generated/json/mybean/WeatherModel.dart';
import 'package:wk_weather/utils/ExtensionTool.dart';
import 'package:wk_weather/views/widget/BlurRectWidget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';

import 'package:url_launcher/url_launcher.dart';

/// 生活指数
class LifeIndexView extends StatelessWidget {
  WeatherModelResultDailyLifeIndex modelResultDailyLifeIndex;
  List<LifeIndexDetail> lifeIndexDetail = [];
  final String skycon;

  LifeIndexView({Key key, this.modelResultDailyLifeIndex, this.skycon})  : super(key: key) {
    if (modelResultDailyLifeIndex != null) {
      var now = DateTime.now();
        // 紫外线指数
        modelResultDailyLifeIndex.ultraviolet.forEach((element) {
          // 紫外线时间   "date": "2020-11-17T00:00+08:00",
          var time = element.date.dateTime;
          if (time.day ==now.day) {
            lifeIndexDetail.add( LifeIndexDetail(
                LifeIndexType.ultraviolet, element.desc, element.date));
          }
        });

        // 洗车指数
        modelResultDailyLifeIndex.carWashing.forEach((element) {
          var time = element.date.dateTime;
          if (time.day == now.day) {
            lifeIndexDetail.add(LifeIndexDetail(
                LifeIndexType.carWashing, element.desc, element.date));
          }
        });

        // 穿衣指数
        modelResultDailyLifeIndex.dressing.forEach((element) {
          var time = element.date.dateTime;
          if (time.day == now.day) {
            lifeIndexDetail.add(LifeIndexDetail(
                LifeIndexType.dressing, element.desc, element.date));
          }
        });

        // 舒适度指数
        modelResultDailyLifeIndex.comfort.forEach((element) {
          var time = element.date.dateTime;
          if (time.day == now.day) {
            lifeIndexDetail.add(LifeIndexDetail(
                LifeIndexType.comfort, element.desc, element.date));
          }
        });

        // 感冒指数
        modelResultDailyLifeIndex.coldRisk.forEach((element) {
          var time = element.date.dateTime;
          if (time.day == now.day) {
            lifeIndexDetail.add(LifeIndexDetail(
                LifeIndexType.coldRisk, element.desc, element.date));
          }
        });

        lifeIndexDetail.add(LifeIndexDetail(
            LifeIndexType.typhoon, "详情", ""));
    }
  }

  @override
  Widget build(BuildContext context) {
    var itemWidth = (1.wp - DimenConstant.mainMarginStartEnd * 2) / 3 ;

    var items = BlurRectWidget(
      child: Container(
        height: itemWidth * 1.6      ,
        child: GridView.count(
          childAspectRatio: 1.5, // 子控件宽高比
          physics: NeverScrollableScrollPhysics(),
          crossAxisCount: 3, // 每一行的个数
          children: lifeIndexDetail
              .map((e) => _buildLifeIndexItem(context, e))
              .toList(),
        ),
      ),
    );;

    return  items ;
  }

  Widget _buildBottomSheetIndexItem(BuildContext context, LifeIndexDetail detail) {
    String showTime = "${TimeUtil.getWeatherDayDesc(detail.date)}";
    return GestureDetector(
      child: Container(
        child: Column(
          children: <Widget>[
            Text(
              "$showTime：${detail.desc}",
              style: TextStyle(color: Colors.white, fontSize: 13),
            ),
          ],
        ),
      )
    );
  }

  Widget _buildLifeIndexItem(BuildContext context, LifeIndexDetail detail) {
    // fixme InkWell 水波纹 控件, 水波纹没有效果，原因不明
    var inkwell =InkWell(
      child: Container(
        child:   Column(
          children: <Widget>[
            Image.asset(
              WeatherUtils.getLifeIndexIcon(detail.type),
              width: 30,
              height: 30,
            ),
            SizedBox(
              height: 20,
            ),

            Text(
              "${WeatherUtils.getLifeIndexDesc(detail.type)}：${detail.desc}",
              style: TextStyle(color: Colors.white54, fontSize: 13),
            ),
          ],
        ) ,


      ),
      splashColor: Colors.red,
      highlightColor: Colors.blue,
      onTap: () {
        if (detail.type != LifeIndexType.typhoon) {

          showMaterialModalBottomSheet(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.only( topLeft: Radius.circular(30), topRight: Radius.circular(30)),
            ),
            backgroundColor: Colors.transparent,
            context: context,
            builder: (context, scrollController) => BlurRectWidget(
              color: WeatherUtils.getColor(WeatherUtils.convertWeatherType(skycon))[0].withAlpha(80),
              child: Container(
                height: 0.2.hp,
                child: _buildSheetWidget(context, detail.type),
              ),
            ),
          );
        } else {
          _launchURL("http://typhoon.zjwater.gov.cn/wap.htm");
        }
      },
    );

    var inkview =Ink(
      decoration: new BoxDecoration(
        color: Colors.purple,
        borderRadius: new BorderRadius.all(new Radius.circular(25.0)),
      ),
      child:inkwell ,
    ) ;

    return  inkview ;
  }
  _launchURL(var url) async {
    if (await canLaunch(url)) {
      await launch(url);
    }
  }

  Widget _buildSheetWidget(BuildContext context, LifeIndexType lifeIndexType) {
    List<LifeIndexDetail> detail = [];
    if (modelResultDailyLifeIndex != null) {

        if (lifeIndexType == LifeIndexType.ultraviolet) {
          modelResultDailyLifeIndex.ultraviolet.forEach((element) {
            detail.add(LifeIndexDetail(
                LifeIndexType.ultraviolet, element.desc, element.date));
          });
        }

        if (lifeIndexType == LifeIndexType.carWashing) {
          modelResultDailyLifeIndex.carWashing.forEach((element) {
            detail.add(LifeIndexDetail(
                LifeIndexType.carWashing, element.desc, element.date));
          });
        }

        if (lifeIndexType == LifeIndexType.dressing) {
          modelResultDailyLifeIndex.dressing.forEach((element) {
            detail.add(LifeIndexDetail(
                LifeIndexType.dressing, element.desc, element.date));
          });
        }

        if (lifeIndexType == LifeIndexType.comfort) {
          modelResultDailyLifeIndex.comfort.forEach((element) {
            detail.add(LifeIndexDetail(
                LifeIndexType.comfort, element.desc, element.date));
          });
        }

        if (lifeIndexType == LifeIndexType.coldRisk) {
          modelResultDailyLifeIndex.coldRisk.forEach((element) {
            detail.add(LifeIndexDetail(
                LifeIndexType.coldRisk, element.desc, element.date));
          });
        }
    }


    return Container(
      margin: EdgeInsets.only(top: 10),
      child: GridView.count(
        childAspectRatio: 2,
        physics: NeverScrollableScrollPhysics(),
        crossAxisCount: 4,
        children: detail
            .map((e) => _buildBottomSheetIndexItem(context, e))
            .toList(),
      ),
    );
  }


}

class LifeIndexDetail {
  LifeIndexType type;
  String desc;
  String date;

  LifeIndexDetail(this.type, this.desc, this.date);
}
